import pygame
from plane import *
from bullet import *


class PLaneGame(object):
    def __init__(self):
        # 1.创建一个窗口，用来显示内容
        self.screen = pygame.display.set_mode((480, 700), 0, 32)

        # 2.创建一个和窗口大小的图片，用来充当背景
        self.image_file_path = './feiji/background.png'
        self.background = pygame.image.load(self.image_file_path).convert()

    def __create_sprites(self):
        # 3. 创建一个飞机对象
        hero_plane = HeroPlane(self.screen, "hero")

        # 4. 创建一个敌人飞机
        enemy_plane = EnemyPlane(self.screen, "enemy")
        self.start_game(hero_plane, enemy_plane)

    def start_game(self, hero_plane, enemy_plane):
        print("游戏开始...")
        while True:
            self.screen.blit(self.background, (0, 0))
            hero_plane.display()
            enemy_plane.display()
            enemy_plane.move()
            enemy_plane.launch_bullet()

            # 判断是否是点击了退出按钮
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    print("exit")
                    exit()
                elif event.type == pygame.KEYDOWN:
                    if event[pygame.K_LEFT]:
                        print('left')
                        # 控制飞机让其向左移动
                        hero_plane.move_left()
                    elif event[pygame.K_d] or event[pygame.K_RIGHT]:
                        print('right')
                        hero_plane.move_right()
                    elif event[pygame.K_SPACE]:
                        print('space')
                        hero_plane.launch_bullet()


if __name__ == '__main__':
    PLaneGame()
